﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chat.Business.Service.NetP2P;
using Chat.Business.Service.DataContracts;
using Chat.Business.Service.BasicHttp;
using VMuktiService;
using System.ServiceModel;
//using Video.Business.Service.BasicHttp;
//using Video.Business.Service.DataContracts;
//using Video.Business.Service.NetP2P;
using System.Runtime.Remoting;
using System.Globalization;
using System.Reflection;
using VMuktiAPI;
using System.Security.Permissions;

namespace Chat.Presentation
{
    [Serializable]
    public class DummyClient
    {
        public string UserName;
        public int MyId;
        List<AppDomain> appDummyDomains = new List<AppDomain>();

        public static List<object> objChatDummies = new List<object>();        

        public DummyClient(string uname)
		{
			try
			{
				this.UserName = uname;
			}
			catch (Exception exp)
			{
				exp.Data.Add("My Key", "DummyClient()--:--DunnyClient.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);
			}
        }

        public string ChatClient(int ID, string netP2pUri, string strChatSNodeIp)
		{
			try
			{
                //string httpUri = "http://" + strChatSNodeIp + ":80/VMukti/Chat1";
                string httpUri = "http://" + strChatSNodeIp + ":80/VMukti/Chat" + (objChatDummies.Count + 1).ToString() + "/" + DateTime.Now.ToUniversalTime().Millisecond.ToString();
                AppDomainSetup setup = new AppDomainSetup();
                setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory ;
                appDummyDomains.Add(AppDomain.CreateDomain("DummyChats" + ID.ToString(), null, setup, new System.Security.PermissionSet(PermissionState.Unrestricted)));
                appDummyDomains[appDummyDomains.Count - 1].ApplicationTrust.ExtraInfo = AppDomain.CurrentDomain.ApplicationTrust.ExtraInfo;
                appDummyDomains[appDummyDomains.Count - 1].ApplicationTrust.DefaultGrantSet = new System.Security.Policy.PolicyStatement(new System.Security.PermissionSet(PermissionState.Unrestricted));
                appDummyDomains[appDummyDomains.Count - 1].ApplicationTrust.IsApplicationTrustedToRun = true;
                appDummyDomains[appDummyDomains.Count - 1].ApplicationTrust.Persist = true;
              
				objChatDummies.Add(InstantiateDecimal(appDummyDomains[appDummyDomains.Count - 1], new DomainBinder(), new CultureInfo("en-US"), UserName, "", ID, netP2pUri, httpUri));
				return httpUri;            

			}
			catch (Exception exp)
			{
				exp.Data.Add("My Key", "ChatClient()--:--DummyClient.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);
                return null;
			}
        }
       
        public string ChatClientWithoutDummy(int ID, string netP2pUri, string strChatSNodeIp)
		{
            try
            {
                string httpUri = "http://" + strChatSNodeIp + ":80/VMukti/Chat" + (objChatDummies.Count + 1).ToString() + "/" + DateTime.Now.ToUniversalTime().Millisecond.ToString();
                objChatDummies.Add(new ChatDummy(UserName, "", ID, netP2pUri, httpUri));
                return httpUri;
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "ChatClientWithoutDummy()--:--DummyClient.cs--:--" + exp.Message + " :--:--");
                ClsException.LogError(exp);
                ClsException.WriteToErrorLogFile(exp);
                return null;
            }
			
        }

        static object InstantiateDecimal(AppDomain appDomain, Binder binder, CultureInfo cultureInfo,string MyName, string UName, int Id, string netP2pUri, string httpUri)
        {
            try
            {              

                object instance = appDomain.CreateInstance(
                   "Chat.Presentation",
                   "Chat.Presentation.ChatDummy",
                   false,
                   BindingFlags.Default,
                   binder,
                   new object[] {MyName, UName, Id, netP2pUri, httpUri },
                   cultureInfo,
                   null,
                   null
                );
                return instance;
            }
            catch(Exception exp)
            {               
				exp.Data.Add("My Key", "InstantiateDecimal()--:--DummyClient.cs--:--" + exp.Message + " :--:--");
				ClsException.LogError(exp);
				ClsException.WriteToErrorLogFile(exp);			
                return null;
            }
        }   

    }
}
